﻿using System;
using System.Configuration;
using System.Web.UI;
using BUS;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Net.Configuration;
using DTO;
using System.Xml;

namespace WebRaoVat
{
    public partial class QuenMatKhau : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = ((XmlElement)XL_XML.ReadNodeSingle(XL_XML.ReadDocumentFile(Server.MapPath("Gui.xml")),
            "/GIAODIEN/THEME")).GetAttribute("ten");
        }

        public List<string> ListMessage = new List<string>();

        protected void btnDongY_Click(object sender, EventArgs e)
        {
            ListMessage.Clear();
            string strEmail = txtEmail.Text.Trim();

            if (NguoiDungBUS.kiemTraEmailTrung(strEmail) == false)
            {
                ListMessage.Add("* Email này chưa tồn tại trong hệ thống chúng tôi");
                return;
            }

            StreamReader sr = new StreamReader(Server.MapPath("TemplateMail/TemplateQuenMatKhau.htm"));

            sr = File.OpenText(Server.MapPath("TemplateMail/TemplateQuenMatKhau.htm"));

            string content = sr.ReadToEnd();

            content = content.Replace("[Sender]", "B1_Group");

            string noiDung = "";
            const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            Random rand = new Random();

            for (int i = 0; i < 8; ++i)
            {
                string str = chars[rand.Next(chars.Length)].ToString();
                noiDung += str;
            }

            content = content.Replace("[Content]", "Mật khẩu mới: " + noiDung);
            content = content.Replace("[DateTime]", DateTime.Now.ToString());

            if (SendMail("Lấy lại mật khẩu !", content, strEmail, true, false) == false)
            {
                ListMessage.Add("* Gửi mail không thành công");
                return;
            }
            else
            {
                try
                {
                    NguoiDungDTO nguoiDung = NguoiDungBUS.layNguoiDungTheoEmail(strEmail);
                    nguoiDung.MatKhau = noiDung;

                    NguoiDungBUS.Update(nguoiDung);
                    ListMessage.Add("Đã gởi !");
                }
                catch (System.Exception ex)
                {
                    ListMessage.Add("* Cập nhật mật khẩu không thành công !");
                }
            }
        }

        public static string FormAddress
        {
            get
            {
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                return cfg.Network.UserName;
            }
        }

        public bool SendMail(string subject, string body, string to, bool isHtml, bool isSSL)
        {
            try
            {
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(FormAddress, "Rao vặt - B1 Group");
                    mail.To.Add(to);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = isHtml;
                    SmtpClient client = new SmtpClient();
                    client.EnableSsl = isSSL;
                    client.Send(mail);
                }
            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }
    }
}
